package example.bean.learn;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Calculate extends HttpServlet{

	/** 
	* @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
	*/ 
	private static final long serialVersionUID = -3666504053949605203L;
	
	/** 
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
     *      response) 
     */  
    protected void doGet(HttpServletRequest request,  
            HttpServletResponse response) throws ServletException, IOException {  
        doPost(request, response);  
    }  
  
    /** 
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
     *      response) 
     */  
    protected void doPost(HttpServletRequest request,  
            HttpServletResponse response) throws ServletException, IOException {  
  
        request.setCharacterEncoding("utf-8");  
        response.setCharacterEncoding("utf-8");  
        response.setContentType("text/html");  
          
        PrintWriter out = response.getWriter();  
          
  
        String firstParam = request.getParameter("firstParam");  
        String secondParam = request.getParameter("secondParam");  
        String operator = request.getParameter("operator");  
  
        CalBean cal = new CalBean();  
  
  
        if (firstParam != null && cal.isNum(firstParam) && secondParam != null  
                && cal.isNum(secondParam)) {  
  
            if (!firstParam.isEmpty()) {  
                cal.setFirstParam(Double.valueOf(firstParam));  
            }  
  
            if (!secondParam.isEmpty()) {  
                cal.setSecondParam(Double.valueOf(secondParam));  
            }  
            cal.setOperator(operator);  
  
            if (cal.getOperator().equals("/") && cal.getSecondParam() == 0.0) {  
  
                out.println("<html><head>");  
                out.println("<title>" + "简易计算器" + "</title>");  
                out.println("</head>");  
                out.println("<body>");  
                out.println("输入有误，除数不能为零" + "<br>");  
                out.println("<a href='index.html'>返回</a>");  
                out.println("</body>");  
                out.println("</html>");  
  
            } else {  
                double result = cal.calculate();  
  
                out.println("<html><head>");  
                out.println("<title>" + "简易计算器" + "</title>");  
                out.println("</head>");  
                out.println("<body>");  
                out.println(firstParam + " " + operator + " " + secondParam  
                        + "= " + result + "<br>");  
                out.println("<a href='index.html'>返回</a>");  
                out.println("</body>");  
                out.println("</html>");  
            }  
  
        } else {  
            out.println("<html><head>");  
            out.println("<title>" + "简易计算器" + "</title>");  
            out.println("</head>");  
            out.println("<body>");  
            out.println("输入有误，无法进行计算，请输入数字且除数不能为零" + "<br>");  
            out.println("<a href='index.html'>返回</a>");  
            out.println("</body>");  
            out.println("</html>");  
  
        }  
    }  

	public Calculate() {
		// TODO Auto-generated constructor stub
	}

}
